<template>
	<view style="display: flex;flex-direction: column;">
		<view style="display: flex; flex-direction: column; flex-shrink: 0; margin: 50rpx 10rpx 0 10rpx">
			<zero-markdown-view :markdown="content"></zero-markdown-view>
		</view>
		<view style="margin: 10px 30px 30px 30px;">
			<uv-button v-if="ad" @click="showVideoAd" type="success">{{ btntxt }}</uv-button>
		</view>
		<uv-line color="#000000"></uv-line>
		<view style="margin: 20rpx 10rpx 20rpx 10rpx">
			<ad v-if="showad" :unit-id="config.FullVideoID" ad-type="video" ad-theme="white"></ad>
		</view>
		<uv-toast ref="toast"></uv-toast>
	</view>
</template>

<script setup>
	import {
		onMounted,
		ref
	} from 'vue';
	import {
		onShow
	} from '@dcloudio/uni-app'
	import tools from "../../utils/tools.js";
	import gdatas from "../../utils/data.js";
	import config from '../../common/config.js';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	let interstitialAd = null;
	let isInterstitialLoading = false;
	let rewardedVideoAd = null;
	let isVideoAdLoading = false;
	let modal = ref(null)
	let encryption = false;
	let adhint = ref("")
	let title = ref("");
	let ad = ref(true);
	let content = ref("");
	let page1 = "";
	let page2 = "";
	let link = "";
	let showad = ref(false)
	let btntxt = ref("")
	let toast = ref(null)

	function adconfirm() {
		showVideoAd();
	}

	function adcancel() {

	}

	function getPage1() {
		uni.request({
			url: config.giteeUrl + page1,
			success: (res) => {
				content.value = res.data
			}
		})
	}

	function showToast(params) {
		toast.value.show({
			...params,
			complete() {}
		})
	}


	function getPage2() {
		uni.request({
			url: config.giteeUrl + page2,
			success: (res) => {
				content.value = res.data
			}
		})
	}



	onLoad(async (op) => {
		console.log(op)
		//从码云读取数据
		uni.request({
			url: config.giteeUrl + "ad/index.dat",
			success: (res) => {
				let datas = res.data[op.id]
				encryption = datas.encryption;
				ad.value = datas.ad;
				btntxt.value = datas.btntxt;
				page1 = datas.page;
				page2 = datas.page2;
				getPage1()
			}
		})
	})

	onShow(() => {
		showad.value = true;
		//插屏广告实例
		if (wx.createInterstitialAd) {
			interstitialAd = wx.createInterstitialAd({
				adUnitId: config.InterstitialID
			})
			interstitialAd.onLoad(() => {
				console.log('插屏广告加载')
				if (interstitialAd && !isInterstitialLoading) {
					interstitialAd.show().catch((err) => {
						console.error('插屏广告显示失败', err)
						// if (isVideoAdLoading == true) {
						// 	showVideoAd();
						// }
					})
				}
			})
			interstitialAd.onError((err) => {
				console.error('插屏广告加载失败', err)
				if (isVideoAdLoading == true) {
					showVideoAd();
				}
			})
			interstitialAd.onClose(() => {
				console.log('插屏广告关闭')
				isInterstitialLoading = false
				// if (isVideoAdLoading == true) {
				// 	showVideoAd();
				// }
			})
		}
	})

	onMounted(() => {

		//激励广告
		if (wx.createRewardedVideoAd) {
			rewardedVideoAd = wx.createRewardedVideoAd({
				adUnitId: config.RewardedVideoID
			})
			rewardedVideoAd.onLoad(() => {
				console.log('激励广告加载成功');
				isVideoAdLoading = true
			})
			rewardedVideoAd.onError((e) => {
				console.error('激励视频广告加载失败', e)
				showToast({
					type: 'default',
					message: e,
				})
			})
			rewardedVideoAd.onClose((e) => {
				// 用户点击了关闭广告
				console.log("激励广告关闭", e);
				if (e && e.isEnded) {
					ad.value = false;
					getPage2()()
				} else {
					showToast({
						type: 'default',
						message: '激励广告未观看完成',
					})
				}
			})
		}
	})

	function showVideoAd() {
		if (isVideoAdLoading) {
			isVideoAdLoading = false;
			rewardedVideoAd.show();
		} else {
			showToast({
				type: 'default',
				title: '激励广告未加载完成',
				message: "激励广告未加载完成",
			})
		}
	}
</script>

<style>

</style>